//
//  TZWWelComeViewController.swift
//  Sinaweibotzw
//
//  Created by 檀志文 on 16/4/12.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

import UIKit

import SnapKit

import SDWebImage


//    swift 定义枚举，使用原始值 0
enum Season: Int{
    case Spring = 0
    case Summer
    case Autumn
    case Winter
}
class TZWWelComeViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareUI()
        
        setIcon()
        
        // 发送网络请求,获取用户信息
        TZWUserAccoutViewModel.sharedInstance.loadUseInfo { (error) -> () in
            if error != nil {
                 HMPrint("获取用户信息出错了: \(error)")
                return
            }
            self.setIcon()
        }
    }
    
//    设置头像
    private func setIcon(){
        if let avatar_large = TZWUserAccoutViewModel.sharedInstance.userAccount?.avatar_large {
//            网络请求完成调用这段代码，sd加载图片
            let url = NSURL(string: avatar_large)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    private func prepareUI() {
//        1 添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
//        2 添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
        }
        //label
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
    // 在view看得见的时候做动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconViewAnimation()
    }

    // 头像动画
    private func iconViewAnimation() {
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset(160 - (UIScreen.mainScreen().bounds.height))
        }
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 通知系统来重新设置布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                // 让label显示出来
                UIView.animateWithDuration(HMDefautlAnimDuration, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        HMPrint("动画完成了,要切换界面")
//                        切换到主控制器
                        let controller = TZWMainTableViewController()
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(controller)
                })
        }
    }
    
    
//    懒加载(背景)
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
//    头像
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    // 圆角 170(2x) 85
        imageView.layer.cornerRadius = 42.5
        //剪裁
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    // 欢迎的label
    private lazy var welcomeLabel: UILabel = {
    
        let label = UILabel()
        // 设置
        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(WelcomeLabelFontSize)
        label.sizeToFit()
        label.alpha = 0
        
        return label
    }()
    
}
